package cn.enjoy.rocketmq.config;

import cn.enjoy.rocketmq.prop.AliyunMqProperties;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author K
 */
@Configuration
@EnableConfigurationProperties(AliyunMqProperties.class)
public class AliyunMqConfig {

    /**
     * 创建mq生产者
     *
     * @return 生产者bean
     */
    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ProducerBean createProducer(AliyunMqProperties AliyunMqProperties) {
        ProducerBean producerBean = new ProducerBean();
        Properties properties = new Properties();
        //阿里云 Access Key
        properties.setProperty(PropertyKeyConst.AccessKey, AliyunMqProperties.getAccessKey());
        //阿里云 Access secret key
        properties.setProperty(PropertyKeyConst.SecretKey, AliyunMqProperties.getSecretKey());
        //阿里云mq 地址
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, AliyunMqProperties.getNameAddress());
        //阿里云mq groupId
        properties.setProperty(PropertyKeyConst.GROUP_ID, AliyunMqProperties.getGroupId());
        producerBean.setProperties(properties);

        return producerBean;
    }

}
